﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using HelperPrototype.TabPageControls;

namespace HelperPrototype
{
    public partial class Main2 : Form
    {
        public Main2()
        {
            InitializeComponent();
        }

        private void Main_Load(object sender, EventArgs e)
        {
            foreach (Control ctl in this.Controls)
                if (ctl.GetType() == typeof(MdiClient))
                    ctl.BackColor = Color.WhiteSmoke;


            // check login
            if (Program.MaNguoiDungHienTai == null)
            {
                LoginForm f = new LoginForm();
                f.ShowDialog();
            }

            // ok, expand tree
            treeView1.ExpandAll();
            if (Program.MaNguoiDungHienTai == "x")
                return;

            // check quyen han
            _DB.DataClasses1DataContext dc = new _DB.DataClasses1DataContext();
            _DB.NguoiDung u = dc.NguoiDungs.Single(x => x.MaNguoiDung == Program.MaNguoiDungHienTai);
            foreach (TreeNode n in treeView1.Nodes)
            for (int i = n.Nodes.Count - 1; i >= 0; i--)
            {
                TreeNode node = n.Nodes[i];
                if (node.Tag == null)
                    continue;

                try
                {
                    int quyenID = int.Parse(((string)node.Tag).Split(',').First());
                    int c = u.NhomNguoiDung.NhomNguoiDung_QuyenHans.Count(x => x.QuyenHanID == quyenID);
                    if (c == 0)
                        n.Nodes.RemoveAt(i);
                }
                catch (System.Exception ex)
                { 
                    n.Nodes.RemoveAt(i); 
                }
            }
        }


        public void addNewTab(String tabName, UserControl userControl)
        {
            // check old window
            foreach (Form ff in this.MdiChildren)
            {
                Type t = ff.Controls[0].GetType();
                if (t == userControl.GetType())
                {
                    ff.Focus();
                    return;
                }
            }

            // open new window
            Form f = new Form();
            f.Text = tabName;
            f.Width = (this.Width - treeView1.Width) * 80 / 100;
            f.Height = 300;
            f.Controls.Add(userControl);
            userControl.Dock = DockStyle.Fill;
            f.MdiParent = this;
            f.Show();
            
        }

        public void closeTab()
        {
            /*
            if (tabControl1.SelectedTab == null)
                return;

            int i = tabControl1.SelectedIndex;
            tabControl1.SelectedIndex = (tabControl1.TabCount >= i + 2) ? i + 1 : i - 1;
            tabControl1.TabPages.Remove(tabControl1.TabPages[i]);
        
             */ 
        }

        private void treeView1_DoubleClick(object sender, EventArgs e)
        {
            // check
            TreeView tree = (TreeView) sender;
            if (tree.SelectedNode.Tag == null)
                return;

            // get tabpage class name
            String name = ((string)tree.SelectedNode.Tag).Split(',').Last();
            if (name == null)
                return;

            // go
            Type t = Type.GetType("HelperPrototype." + name);
            if (t != null)
            {
                UserControl u = (UserControl) Activator.CreateInstance(t);
                if (u != null)
                    addNewTab(tree.SelectedNode.Text, u);
                else
                    MessageBox.Show("Can create user control " + name);
            }
            else
                MessageBox.Show("There're no " + name);

        }

        private void settingToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OtherForms.SettingForm f = new OtherForms.SettingForm();
            f.ShowDialog();
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void đăngXuấtToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Program.MaNguoiDungHienTai = null;
            this.Close();
        }


        /////////////////////////////////////
        // skin
        /////////////////////////////////////

        private void diamondBlueToolStripMenuItem_Click(object sender, EventArgs e)
        {
            skinEngine1.ApplyAdditionalBuiltInSkins(0);
        }

        private void diamondRedToolStripMenuItem_Click(object sender, EventArgs e)
        {
            skinEngine1.ApplyAdditionalBuiltInSkins(1);
        }

        private void macOSToolStripMenuItem_Click(object sender, EventArgs e)
        {
            skinEngine1.ApplyAdditionalBuiltInSkins(2);
        }

        private void mediaPlayer10ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            skinEngine1.ApplyAdditionalBuiltInSkins(3);
        }

        private void office2007ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            skinEngine1.ApplyAdditionalBuiltInSkins(4);
        }

        private void sportGreenToolStripMenuItem_Click(object sender, EventArgs e)
        {
            skinEngine1.ApplyAdditionalBuiltInSkins(5);
        }

        private void steelBlackToolStripMenuItem_Click(object sender, EventArgs e)
        {
            skinEngine1.ApplyAdditionalBuiltInSkins(6);
        }

        private void vista2Color4ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            skinEngine1.ApplyAdditionalBuiltInSkins(7);
        }

        private void waveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            skinEngine1.ApplyAdditionalBuiltInSkins(8);
        }

    }
}
